<template>
  <!-- 添加或修改付款申请单对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-row :gutter="10">
      <el-col :span="8">
        <el-card class="box-card">
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row :gutter="20">
              <el-col :span="24">
                <el-form-item label="申请时间" prop="applyTime">
                  <el-date-picker clearable
                                  v-model="form.applyTime"
                                  type="datetime"
                                  value-format="yyyy-MM-dd HH:mm:ss"
                                  placeholder="请选择申请时间">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="发票编号" prop="invoiceCode">
                  <el-input v-model="form.invoiceCode" placeholder="请输入发票编号"/>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="发票照片" prop="invoicePhoto">
                  <image-upload v-model="form.invoicePhoto" :limit="1"></image-upload>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="申请金额:" prop="totalMoney">
                  <el-input-number v-model="form.totalMoney" :precision="2" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="备注" prop="remark">
                  <el-input type="textarea" v-model="form.remark" placeholder="请输入备注"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </el-col>
      <el-col :span="16">
        <el-row :gutter="10">
          <el-col :span="24">
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span>供应商信息</span>
              </div>
              <el-descriptions class="margin-top " :contentStyle="CS" :label-style="LS" title="" :column="2" size="medium" border>
                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-user"></i>
                    供应商名称
                  </template>
                  {{buy.supplierName}}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-money"></i>
                    供应商银行
                  </template>
                  {{buy.bankName}}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-c-scale-to-original"></i>
                    供应商行号
                  </template>
                  {{buy.bankNo}}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-bank-card"></i>
                    供应商账号
                  </template>
                  {{buy.accountNo}}
                </el-descriptions-item>

                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-c-scale-to-original"></i>
                    货款总额
                  </template>
                  {{buy.money}}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-bank-card"></i>
                    已付定金
                  </template>
                  {{buy.dingjin}}
                </el-descriptions-item>

              </el-descriptions>
            </el-card>
          </el-col>

          <el-col :span="24" style="padding-top: 10px;">
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span>采购明细</span>
              </div>
              <el-table border v-loading="loading" :data="buyDetailList"  ref="table"
                        :max-height="tableMaxHeight">
                <el-table-column label="序号" type="index" align="center" width="50" fixed="left"/>
                <el-table-column label="订单名称" align="center" prop="projectName"/>
                <el-table-column label="产品名称" align="center" prop="goodsName"  show-overflow-tooltip/>
                <el-table-column label="产品型号" align="center" prop="goodModel"/>
                <el-table-column label="产品明细" align="center" prop="goodsBrand"/>
                <el-table-column label="产品单位" align="center" prop="unit"/>
                <el-table-column label="单价" align="center" prop="goodsBuyPrice"/>
                <el-table-column label="采购数量" align="center" prop="shengyu"/>
                <el-table-column label="含税总价" align="center" prop="goodsBuyTotal"/>
              </el-table>

              <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                          :limit.sync="queryParams.pageSize" @pagination="getBuyDetailList"/>
            </el-card>
          </el-col>
        </el-row>
      </el-col>
    </el-row>


    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addAction, getAction, getById, updateAction} from '@/api/manage';
import moment from 'moment';
import {parseTime} from "@/utils/common";
import CaigouDetaiList from "@/views/develop/lvBuy/module/CaigouDetailList.vue";

export default {
  name: "PayApplyArrearsForm",
  dicts:[],
  components: {CaigouDetaiList},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      buyDetailList:[],
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        cgallid:null,
        pageNum: 1,
        pageSize: 20,
        mainId: null,
        name: null,
        brand: null,
        unit: null,
        costPrice: null,
        count: null,
        cost: null,
        validityTerm: null,
        buyId: null,
        buyName: null,
        buyDate: null,
        buyType: null,
        reimburse: null,
        supplierId: null,
        supplierName: null,
        dateRange: "",
        params: {},
      },
      buy: {},
      // 表单校验
      rules: {
        applyTime: [
          {required: true, message: "请选择申请时间", trigger: "blur"}
        ],
        totalMoney: [
          {required: true, message: "请输入申请付款金额", trigger: "blur"},
          {
            validator:this.totalMoneyValidator,
            trigger: 'blur'
          }
        ],
        invoiceCode: [
          {required: true, message: "请输入发票金额", trigger: "blur"},
        ],
        invoicePhoto: [
          {required: true, message: "请上传发票照片", trigger: "blur"},
        ],
      },
      url: {
        getById: '/payApply/payApply/',
        addForHardWare: '/payApply/payApply/addForHardWare',
        edit: '/payApply/payApply/edit',
        buyDetailList:'/goodsSalebuyall/goodsSalebuyall/list',
      },
      CS: {
        'text-align': 'center',  //文本居中
        'min-width': '250px',   //最小宽度
        'word-break': 'break-all'  //过长时自动换行
      },
      LS: {
        'color': '#000',
        'text-align': 'center',
        'font-weight': '600',
        'height': '40px',
        'min-width': '110px',
        'word-break': 'keep-all'
      }
    };
  },
  created() {

  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        buyId: null,
        applyTime: null,
        invoiceCode: null,
        invoicePhoto: null,
        totalMoney: null,
        paidMoney: null,
        remainMoney: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },

    totalMoneyValidator(rule, value, callback) {
      if (value === 0) {
        callback(new Error('申请金额不能为0'));
      } else {
        callback();
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加付款申请单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改付款申请单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.edit, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok');
            });
          } else {
            addAction(this.url.addForHardWare, this.form).then(response => {
              this.$modal.msgSuccess("付款单申请成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },

    show(row) {
      this.reset();
      this.buy =row;
      this.open = true;
      this.form.applyTime = parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}');
      this.title = "金具付款申请单";
      this.queryParams.mainId=row.id;
      this.form.buyId=row.id;
      this.form.totalMoney=row.arrears;
      this.form.supplierId=row.supplierId;
      this.getBuyDetailList();
    },

    /** 查询销售信息管理列表 */
    getBuyDetailList() {
      this.loading = true;
      getAction(this.url.buyDetailList, this.queryParams).then(response => {
        this.buyDetailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },


  }
};
</script>

